package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.Menu;
import com.lagou.domain.ResponseResult;
import com.lagou.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu(int currentPage, int pageSize){
        PageInfo<Menu> pageInfo = menuService.findAllMenu(currentPage, pageSize);
        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", pageInfo);
        return responseResult;
    }

    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(int id){
        List<Menu> parentMenuList = menuService.findSubMenuListByPid(id);
        Menu menuInfo = null;
        if(id!=-1){
            menuInfo = menuService.findMenuById(id);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("menuInfo", menuInfo);
        map.put("parentMenuList", parentMenuList);
        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", map);
        return responseResult;
    }

    @RequestMapping("/saveOrUpdateMenu")
    public ResponseResult saveOrUpdateMenu(@RequestBody Menu menu){
        if(null == menu.getId()){
            menuService.saveMenu(menu);
            ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", null);
            return responseResult;
        } else {
            menuService.updateMenu(menu);
            ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", null);
            return responseResult;
        }
    }


}
